define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/order/index' + location.search,
                    add_url: 'order/order/add',
                    //edit_url: 'order/order/edit',
                    //del_url: 'order/order/del',
                    multi_url: 'order/order/multi',
                    import_url: 'order/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                // 添加展开图标列
                detailView: true,
                detailFormatter: function (index, row) {
                    var html = '<table class="table table-striped">';
                    html += '<thead><tr>';
                    html += '<th>商品名称</th>';
                    html += '<th>商品图片</th>';
                    html += '<th>商品规格</th>';
                    html += '<th>单价</th>';
                    html += '<th>运费</th>';
                    html += '<th>数量</th>';
                    html += '<th>小计</th>';
                    html += '</tr></thead>';
                    html += '<tbody>';
                    if (row.ordergoods) {
                        row.ordergoods.forEach(function (item) {
                            html += '<tr>';
                            html += '<td>' + item.goods_name + '</td>';
                            html += '<td><img src="' + Fast.api.cdnurl(item.goods_image) + '" style="max-height:60px;"></td>';
                            html += '<td>' + item.spec_name + '</td>';
                            html += '<td>' + item.price + '</td>';
                            html += '<td>' + item.freight_price + '</td>';
                            html += '<td>' + item.quantity + '</td>';
                            html += '<td>' + item.amount + '</td>';
                            html += '</tr>';
                        });
                    }
                    html += '</tbody></table>';
                    return html;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'user.username', title: __('下单用户'), operate: 'LIKE'},
                        {field: 'total_amount', title: __('商品金额'), operate:'BETWEEN'},
                        {field: 'freight_amount', title: __('Freight_amount'), operate:'BETWEEN'},
                        {field: 'order_amount', title: __('Order_amount'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'contact', title: __('Contact'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'ship_time', title: __('Ship_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'express_company', title: __('Express_company'), operate: 'LIKE'},
                        {field: 'express_no', title: __('Express_no'), operate: 'LIKE'},
                        {field: 'complete_time', title: __('Complete_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ship',
                                    text: __('发货'),
                                    title: __('发货'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-truck',
                                    url: 'order/order/ship',
                                    visible: function(row) {
                                        return row.status === '1'; // 已支付状态才显示发货按钮
                                    },
                                    extend: 'data-area=["500px","400px"]'
                                },
                                {
                                    name: 'refund',
                                    text: __('退款'),
                                    title: __('退款'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-reply',
                                    url: 'order/order/refund',
                                    visible: function(row) {
                                        return row.status === '1' || row.status === '3'; // 已支付状态可以退款
                                    },
                                    extend: 'data-area=["500px","400px"]'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            table: table, 
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ship',
                                    text: __('发货'),
                                    title: __('发货'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-truck',
                                    url: 'order/order/ship',
                                    visible: function(row) {
                                        return row.status === '1'; // 已支付状态才显示发货按钮
                                    },
                                    extend: 'data-area=["500px","400px"]'
                                },
                                {
                                    name: 'refund',
                                    text: __('退款'),
                                    title: __('退款'),
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-reply',
                                    url: 'order/order/refund',
                                    visible: function(row) {
                                        return row.status === '1'; // 已支付状态可以退款
                                    },
                                    extend: 'data-area=["500px","400px"]'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        ship: function () {
            Controller.api.bindevent();
            
            // 监听物流公司选择
            $('select[name="express_company"]').change(function(){
                var code = $(this).find("option:selected").data("code");
                $("#express_code").val(code);
            });
        },
        
        add: function () {
            Controller.api.bindevent();
        },
        refund: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
